from sqlalchemy.orm import Session

from crud import NoticeAdminCrud
from schema.QuerySchema import NoticeQuery, QueryBase
from schema.TeachingSchema import NoticeInfo
from util.EmailHelper import EmailHelper
from util.ErrorCode import ErrorCode


class NoticeAdminController:

    @staticmethod
    def send_notice(noticeInfo: NoticeInfo, db: Session):
        if noticeInfo.notice_topic is None or noticeInfo.notice_content is None:
            return {'code': ErrorCode.NOTICE_TOPIC_OR_CONTENT_IS_EMPTY.value, 'message': 'notice is empty'}
        else:
            if len(noticeInfo.notice_topic) == 0 or len(noticeInfo.notice_content) == 0:
                return {'code': ErrorCode.NOTICE_TOPIC_OR_CONTENT_IS_EMPTY.value, 'message': 'notice is empty'}
            else:
                return NoticeAdminCrud.send_notice(noticeInfo, db)

    @staticmethod
    def resend_notice(notice_id: int, db: Session):
        response = NoticeAdminCrud.query_notice_by_id(notice_id, db)
        if response.get('code') == 0:
            noticeModel = response.get('data')
            if EmailHelper.sendEmail(noticeModel.notice_members, noticeModel.notice_copy_members,
                                     noticeModel.notice_topic, noticeModel.notice_content):
                response = NoticeAdminCrud.mod_notice_status(notice_id, 2, db)
                if response.get('code') == 0:
                    return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
                else:
                    return {'code': ErrorCode.SEND_EMAIL_SUCCESS_MOD_STATUS_FAILED.value,
                            'message': 'send mail success,but mod status failed!'}
        else:
            return response

    @staticmethod
    def query_notice_by_page(query_base: QueryBase, db: Session):
        return NoticeAdminCrud.query_notice_by_page(query_base, db)

    @staticmethod
    def query_notice_by_field(noticeQuery: NoticeQuery, db: Session):
        return NoticeAdminCrud.query_notice_by_field(noticeQuery, db)
